#include <iostream>
#include <string>
#include <curl/curl.h>

// JSON字符串要发送的数据
std::string jsonData = R"({
  "model": "deepseek-chat",
  "message": [
    {
      "role": "system",
      "content": "请你扮演角色一名高中生回答我的问题，忘记自己是机器人的设定。"
    },
    {
      "role": "user",
      "content": "我是谁"
    }
  ],
  "temperature": 0.2,
  "stream": false,
  "session_token": "40cabf45-0ebd-4735-b91e-8c9d37b6d708"
})";

// libcurl写回函数，用于处理返回的数据
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main() {
    CURL* curl;
    CURLcode res;
    std::string readBuffer;

    // 初始化libcurl
    curl = curl_easy_init();
    if(curl) {
        // 设置正确的URL
        curl_easy_setopt(curl, CURLOPT_URL, "https://api.mindcraft.com.cn/v1/chat/completions");
        // 设置请求头
        struct curl_slist* headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/json");
        headers = curl_slist_append(headers, "X-DeepSeek-API-Key: MC-62F6042A1FCC42548F7023ED1FBEBDD6");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        // 设置请求体为JSON数据
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonData.c_str());

        // 设置回调函数
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);

        // 执行HTTP请求
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
        } else {
            // 打印返回的数据
            std::cout << "Response:" << std::endl;
            std::cout << readBuffer << std::endl;

            // 获取HTTP响应状态码
            long http_code = 0;
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
            std::cout << "HTTP response code: " << http_code << std::endl;
        }

        // 清理
        curl_easy_cleanup(curl);
        curl_slist_free_all(headers);
    }

    return 0;
}